<template>
    <div>
        <div class="layout-route" v-show="!detailDialogspuinfo.visible">
            <div class="card card-search">
                <el-form :model="queryParams" ref="queryForm" v-show="showSearch" inline label-width="110px">
                    <el-form-item label="商品sku" prop="seller_sku">
                        <el-input v-model.trim="queryParams.seller_sku" placeholder="请输入商品sku" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
                    </el-form-item>
                    <el-form-item label="商品spu" prop="spu">
                        <el-input v-model.trim="queryParams.spu" placeholder="请输入商品spu" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
                    </el-form-item>
                    <el-form-item label="货架位" prop="warehouse_position">
                        <el-input v-model.trim="queryParams.warehouse_position" placeholder="请输入货架位" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
                    </el-form-item>
                    <el-form-item label="创建时间" prop="create_time">
                        <el-date-picker v-model="queryParams.create_time" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 240px">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item label=" ">
                        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
                    </el-form-item>
                </el-form>
            </div>
            <el-row :gutter="8">
                <el-col style="width: 20%">
                    <el-card shadow="hover" class="text-large chart-card">
                        <div class="chart-card-header text-mini" slot="header">在途数量总计</div>
                        <div class="chart-card-content">
                            <span class="content-title">{{ total_on_way_quantity }} </span>
                        </div>
                    </el-card>
                </el-col>
                <el-col style="width: 20%">
                    <el-card shadow="hover" class="text-large chart-card">
                        <div class="chart-card-header text-mini" slot="header">未发数量总计</div>
                        <div class="chart-card-content">
                            <span class="content-title">{{ total_not_send_quantity }} </span>
                        </div>
                    </el-card>
                </el-col>
                <el-col style="width: 20%">
                    <el-card shadow="hover" class="text-large chart-card">
                        <div class="chart-card-header text-mini" slot="header">占用库存</div>
                        <div class="chart-card-content">
                            <span class="content-title">{{ total_using_quantity }} </span>
                        </div>
                    </el-card>
                </el-col>
                <el-col style="width: 20%">
                    <el-card shadow="hover" class="text-large chart-card">
                        <div class="chart-card-header text-mini" slot="header">可用库存</div>
                        <div class="chart-card-content">
                            <span class="content-title">{{ total_inventory_quantity }} </span>
                        </div>
                    </el-card>
                </el-col>
                <el-col style="width: 20%">
                    <el-card shadow="hover" class="text-large chart-card">
                        <div class="chart-card-header text-mini" slot="header">库存总数量总计</div>
                        <div class="chart-card-content">
                            <span class="content-title">{{ total_all_inventory_quantity }} </span>
                        </div>
                    </el-card>
                </el-col>
            </el-row>

            <div class="card">
                <el-row :gutter="10" class="mb8">
                    <el-col :span="1.5">
                        <upload-button :action="action" @complete="getList"></upload-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button type="warning" plain icon="el-icon-download" :loading="download" :disabled="download" @click="handleExport">导出</el-button>
                    </el-col>
                    <right-toolbar :showSearch.sync="showSearch" :columns="columns" @queryTable="getList"></right-toolbar>
                </el-row>
                <el-table v-loading="loading" :data="tableData" border script @sort-change="handleSortChange">
                    <el-table-column label="商品SPU" prop="spu" min-width="100" show-overflow-tooltip v-if="showColumn('spu')">
                        <template slot-scope="scope">
                            <el-link type="primary" @click="showDetailDialogspuinfo(scope.row)">{{ scope.row.spu }}</el-link>
                        </template>
                    </el-table-column>
                    <!-- <el-table-column label="产品名称" prop="product_name" min-width="180" v-if="showColumn('product_name')">
                        <template slot-scope="scope">
                            <div class="ellipsis-2">{{ scope.row.product_name }}</div>
                        </template>
                    </el-table-column> -->
                    <el-table-column label="图片" min-width="90" v-if="showColumn('product_images')">
                        <template slot-scope="scope">
                            <ImageUploadImg v-model="scope.row.product_images" :readOnly="true"></ImageUploadImg>
                        </template>
                    </el-table-column>
                    <!-- <el-table-column label="货架位" prop="warehouse_position" min-width="100" show-overflow-tooltip v-if="showColumn('warehouse_position')"></el-table-column> -->
                    <el-table-column
                        label="安全"
                        prop="safe_quantity"
                        min-width="120"
                        sortable="custom"
                        :sort-orders="['descending', 'ascending']"
                        v-if="showColumn('safe_quantity')"
                    ></el-table-column>
                    <el-table-column
                        label="在途"
                        prop="on_way_quantity"
                        min-width="120"
                        sortable="custom"
                        :sort-orders="['descending', 'ascending']"
                        v-if="showColumn('on_way_quantity')"
                    ></el-table-column>
                    <el-table-column
                        label="未发"
                        prop="not_send_quantity"
                        min-width="120"
                        sortable="custom"
                        :sort-orders="['descending', 'ascending']"
                        v-if="showColumn('not_send_quantity')"
                    ></el-table-column>
                    <el-table-column
                        label="占用"
                        prop="using_quantity"
                        min-width="120"
                        sortable="custom"
                        :sort-orders="['descending', 'ascending']"
                        v-if="showColumn('using_quantity')"
                    ></el-table-column>
                    <el-table-column
                        label="可用"
                        prop="inventory_quantity"
                        min-width="120"
                        sortable="custom"
                        :sort-orders="['descending', 'ascending']"
                        v-if="showColumn('inventory_quantity')"
                    ></el-table-column>
                    <el-table-column
                        label="总量"
                        prop="all_inventory_quantity"
                        min-width="120"
                        sortable="custom"
                        :sort-orders="['descending', 'ascending']"
                        v-if="showColumn('all_inventory_quantity')"
                    ></el-table-column>
                    <el-table-column label="创建时间" prop="create_time" min-width="160" show-overflow-tooltip v-if="showColumn('create_time')"></el-table-column>
                    <el-table-column label="更新时间" prop="update_time" min-width="160" show-overflow-tooltip v-if="showColumn('update_time')"></el-table-column>
                    <el-table-column label="操作" min-width="140" fixed="right">
                        <template slot-scope="scope">
                            <el-button type="text" icon="el-icon-edit" @click="showDetailDialog(scope.row)">编辑</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <pagination v-show="total > 0" :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="getList"></pagination>
            </div>
            <DetailDialog v-model="detailDialog.visible" v-if="detailDialog.visible" :params="detailDialog.data" @success="handleQuery2"></DetailDialog>
        </div>
        <DetailDialogspuinfo v-show="detailDialogspuinfo.visible" :params="detailDialogspuinfo.data" @success="handleQueryspuinfo" @cancel="detailDialogspuinfo.visible = false"></DetailDialogspuinfo>
    </div>
</template>

<script>
import { exportFile } from '@/utils/index'
import { getDetailedList,detailedExport, detailedImport } from '@/api/warehouse'
import { STATUS_LIST, IS_HEAD_LIST } from './constant'
import DetailDialog from './components/DetailDialog.vue'
import DetailDialogspuinfo from './components/DetailDialogspuinfo.vue'
import { parseTime } from '@/utils/quickadmin'
export default {
    name: 'SupplyContact',
    components: { DetailDialog,DetailDialogspuinfo },
    data() {
        return {
            supplyList: [],
            // 遮罩层
            loading: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 表格数据
            tableData: [],
            STATUS_LIST,
            pageInfo: {
                page: 1,
                limit: 10,
            },
            // 查询参数
            queryParams: {
                seller_sku: undefined,
                spu: undefined,
                warehouse_position: undefined,
                create_time: []
            },
            detailDialog: {
                visible: false,
                data: {},
            },
            detailDialogspuinfo: {
                visible: true,
                data: {},
            },
            dict: {
                STATUS_LIST,
                IS_HEAD_LIST,
                spuList: []
            },
            action: process.env.VUE_APP_BASE_API + '/warehouse/DetailedList/import',
            download: false,
            total_inventory_quantity: 0,
            total_not_send_quantity: 0,
            total_using_quantity: 0,
            total_on_way_quantity: 0,
            total_all_inventory_quantity: 0,
            columns: [
                // { label:'ID', prop: 'id', visible: true },
                { label:'SPU', prop: 'spu', visible: true },
                { label:'产品名称', prop: 'product_name', visible: true },
                { label:'图片', prop: 'product_images', visible: true },
                { label:'安全库存', prop: 'safe_quantity', visible: true },
                { label:'在途数量', prop: 'on_way_quantity', visible: true },
                { label:'未发数量', prop: 'not_send_quantity', visible: true },
                { label:'占用库存', prop: 'using_quantity', visible: true },
                { label:'可用库存', prop: 'inventory_quantity', visible: true },
                { label:'总库存', prop: 'all_inventory_quantity', visible: true },
                // { label:'货架位', prop: 'warehouse_position', visible: true },
                { label:'创建时间', prop: 'create_time', visible: true },
                { label:'更新时间', prop: 'update_time', visible: true },
            ]
        }
    },
    async created() {
        this.loading = true
        try {
            // const spuData = await querySupplySpuList()
            // this.dict.spuList = spuData?.data?.list ?? []

            // const SupplyListData = await querySupplyListdown()
            // this.supplyList = SupplyListData?.data ?? []

        } catch (error) {
            this.loading = false
            console.log(error)
        }
        this.getList()
    },
    methods: {
        showColumn(prop) {
            const data = this.columns.find(c => c.prop === prop)
            return data?.visible ?? true
        },
        resetSort() {
            this.queryParams.safe_quantity = undefined
            this.queryParams.on_way_quantity = undefined
            this.queryParams.not_send_quantity = undefined
            this.queryParams.using_quantity = undefined
            this.queryParams.inventory_quantity = undefined
            this.queryParams.all_inventory_quantity = undefined
        },
        handleSortChange({ column, prop, order }) {
            this.resetSort()
            this.queryParams[prop] = order === 'descending' ? 'desc' : order === 'ascending' ? 'asc' : undefined
            this.getList()
        },
        handleExportinfo(row) {
            let contractUrl = row.contact_url;
            if (contractUrl) {
                const downloadUrl = `${process.env.VUE_APP_BASE_API}${contractUrl}`;
                window.open(downloadUrl, '_blank');
            }else{
                this.$message({
                    type: 'warning',
                    message: '合同未上传'
                })
            }
        },
        handleExport() {
            this.download = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            detailedExport(qyparams)
                .then(res => {
                    exportFile(res, () => {
                        this.download = false
                    })
                })
                .catch(err => {
                    this.download = false
                })
        },
        showDetailDialog(row) {
            this.detailDialog.visible = true
            this.detailDialog.data = row
        },
        showDetailDialogspuinfo(row) {
            this.detailDialogspuinfo.visible = true
            this.detailDialogspuinfo.data = row
        },
        downloadExe() {
            exportFile({
                downloadUrl: process.env.VUE_APP_PRINT_URL,
            })
        },
        // deleteItem(row){
        //     this.$confirm('确认要删除该供应商吗?', '警告', {
        //         confirmButtonText: '确定',
        //         cancelButtonText: '取消',
        //         type: 'warning',
        //     }).then(()=>{
        //         deleteSupplyList({id: row.id}).then((res)=>{
        //             this.$message.success(res.msg)
        //             this.getList()
        //         })
        //     })
        // },
        //列表
        getList() {
            this.loading = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            if (qyparams.create_time?.length == 2) {
                qyparams.create_time = [parseTime(qyparams.create_time[0], 'y-m-d'), parseTime(qyparams.create_time[1], 'y-m-d')]
            }

            getDetailedList(qyparams)
                .then(response => {
                    this.tableData = response.data.list
                    this.total = response.data.total
                    this.total_inventory_quantity = response.data?.inventory_quantity_total ?? 0
                    this.total_not_send_quantity = response.data?.not_send_quantity_total ?? 0
                    this.total_using_quantity = response.data?.using_quantity_total ?? 0
                    this.total_on_way_quantity = response.data?.on_way_quantity_total ?? 0
                    this.total_all_inventory_quantity = response.data?.all_inventory_quantity_total ?? 0
                })
                .finally(() => {
                    this.loading = false
                })
        },

        // 搜索按钮操作
        handleQuery() {
            this.pageInfo.page = 1
            this.getList()
        },

        // 搜索按钮操作
        handleQuery2() {
            this.getList()
        },
        handleQueryspuinfo() {
            this.getList()
        },

        // 重置按钮操作
        resetQuery() {
            this.resetForm('queryForm')

            this.handleQuery()
        },
    },
}
</script>
<style lang="scss" scoped>
/* 统计卡片 */
::v-deep .chart-card {
    color: #606266;
    margin: 10px 0;
    .el-card__header,
    .el-card__body {
        padding-left: 32px;
    }
}
.chart-card-num {
    font-size: 30px;
}

.chart-card-content {
    height: 30px;
    .content-title {
        font-weight: bold;
        font-size: 20px;
        color: #303133;
    }
}
</style>
